using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Common;
using Common.Enum;
using MainView.MainControls;
using MainView.Windows;
using SExperiment;

namespace MainView.View
{
	public partial class UCSampleInfo : System.Windows.Controls.UserControl, IComponentConnector
	{
		public delegate void DelegateOnAutoIndexChangedEvent(object sender, EventArgs e);

		public static readonly DependencyProperty IsDecryptProperty = DependencyProperty.Register("IsDecrypt", typeof(bool), typeof(UCSampleInfo), new UIPropertyMetadata(true));

		public bool IsDecrypt
		{
			get
			{
				return (bool)GetValue(IsDecryptProperty);
			}
			set
			{
				SetValue(IsDecryptProperty, value);
			}
		}

		public event DelegateOnAutoIndexChangedEvent OnAutoIndexChanged;

		public UCSampleInfo(Experiment experiment)
		{
			InitializeComponent();
			base.DataContext = experiment;
			UCPatientInfo.SetBinding(UCPatientInfo.IsDecryptProperty, new System.Windows.Data.Binding("IsDecrypt")
			{
				Source = this
			});
			UCPatientInfoForWell.SetBinding(UCPatientInfoForWell.IsDecryptProperty, new System.Windows.Data.Binding("IsDecrypt")
			{
				Source = this
			});
			UCPatientInfoForWell.SetWellData(experiment);
			tabPreTreatment.Visibility = ((experiment.PreTreatment == null) ? Visibility.Collapsed : Visibility.Visible);
		}

		public bool IsInfoForWellSelected()
		{
			return TabItemForWell.IsSelected;
		}

		public void SetInfoForWellSelected()
		{
			TabItemForWell.IsSelected = true;
		}

		public void RefreshSampleInfo(List<Sample> samples)
		{
			UCPatientInfo.ItemsSource = samples;
		}

		public void RefreshSampleType()
		{
			UCPatientInfoForWell.uCWellPlate.RefreshSampleType();
		}

		private void MenuExportWellInfo_Click(object sender, RoutedEventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Title = Utility.GetMessage("Save");
			saveFileDialog.Filter = string.Format("{1}{0}|*.{2}|{3}{0}|*.{3}|{4}{0}|*.{4}", Utility.GetMessage("File"), "Excel", "xlsx", "csv", "txt");
			saveFileDialog.FilterIndex = 1;
			saveFileDialog.InitialDirectory = Utility.GetMyDocumentFolder();
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				base.Cursor = System.Windows.Input.Cursors.Wait;
				switch (saveFileDialog.FilterIndex)
				{
				case 1:
					UCPatientInfo.SaveToExcel(saveFileDialog.FileName);
					break;
				case 2:
				case 3:
					UCPatientInfo.SaveToCsv(saveFileDialog.FileName);
					break;
				}
				base.Cursor = System.Windows.Input.Cursors.Arrow;
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_data_export_sussess"));
			}
		}

		private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (e.Source is System.Windows.Controls.TabControl && TabItemForWell.IsSelected && UCPatientInfoForWell != null)
			{
				UCPatientInfoForWell.RefreshInfo();
			}
		}

		public void SetAutoIndex_ForWell()
		{
			UCPatientInfoForWell.OnAutoIndex += AutoIndex_Executed;
		}

		public void AutoIndex_Executed(object sender, EventArgs e)
		{
			UCPatientInfo.dataGrid.CommitEdit();
			AutoIndexWindow autoIndexWindow = new AutoIndexWindow();
			autoIndexWindow.OnAutoIndexChanged += win_OnAutoIndexChanged;
			autoIndexWindow.AllSamples = UCPatientInfo.dataGrid.ItemsSource as List<Sample>;
			if (IsInfoForWellSelected())
			{
				List<Sample> samples = (from s in UCPatientInfoForWell.SelectedWells
					where s.Sample != null
					select s.Sample).Distinct().ToList();
				samples = GetSortedSamples(samples);
				autoIndexWindow.SelectSamples = samples;
			}
			else
			{
				autoIndexWindow.SelectSamples = UCPatientInfo.dataGrid.SelectedCells.Select((DataGridCellInfo s) => s.Item).Distinct().Cast<Sample>()
					.ToList();
			}
			autoIndexWindow.Owner = Utility.GetMainWindow();
			autoIndexWindow.ShowDialog();
			if (autoIndexWindow.IsModify && this.OnAutoIndexChanged != null)
			{
				this.OnAutoIndexChanged(this, new EventArgs());
			}
		}

		private void win_OnAutoIndexChanged(object sender, EventArgs e)
		{
			UCPatientInfoForWell.RefreshInfo();
		}

		private List<Sample> GetSortedSamples(List<Sample> samples)
		{
			Experiment experiment = base.DataContext as Experiment;
			if (experiment != null)
			{
				switch (experiment.ExperimentSetting.SampleSortMode)
				{
				case ESampleSortMode.Column:
					samples = samples.OrderBy((Sample s) => s.Wells[0].ColumnIndex).ToList();
					break;
				case ESampleSortMode.Row:
					samples = samples.OrderBy((Sample s) => s.Wells[0].RowIndex).ToList();
					break;
				case ESampleSortMode.Module:
					samples = (from s in samples
						orderby s.Wells[0].RowIndex
						orderby s.Wells[0].ModelA descending
						select s).ToList();
					break;
				}
			}
			return samples;
		}

		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		[DebuggerNonUserCode]
		internal Delegate _CreateDelegate(Type delegateType, string handler)
		{
			return Delegate.CreateDelegate(delegateType, this, handler);
		}
	}
}
